#include <Arduino.h>
#include "weather.h"
#include "config.h"
#include <ArduinoJson.h>

#include <HTTPClient.h>
#include <string.h>

// 0	晴（国内城市白天晴）	Sunny
// 1	晴（国内城市夜晚晴）	Clear
// 2	晴（国外城市白天晴）	Fair
// 3	晴（国外城市夜晚晴）	Fair
// 4	多云	Cloudy
// 5	晴间多云	Partly Cloudy
// 6	晴间多云	Partly Cloudy
// 7	大部多云	Mostly Cloudy
// 8	大部多云	Mostly Cloudy
// 9	阴	Overcast
// 10	阵雨	Shower
// 11	雷阵雨	Thundershower
// 12	雷阵雨伴有冰雹	Thundershower with Hail
// 13	小雨	Light Rain
// 14	中雨	Moderate Rain
// 15	大雨	Heavy Rain
// 16	暴雨	Storm
// 17	大暴雨	Heavy Storm
// 18	特大暴雨	Severe Storm
// 19	冻雨	Ice Rain
// 20	雨夹雪	Sleet
// 21	阵雪	Snow Flurry
// 22	小雪	Light Snow
// 23	中雪	Moderate Snow
// 24	大雪	Heavy Snow
// 25	暴雪	Snowstorm
// 26	浮尘	Dust
// 27	扬沙	Sand
// 28	沙尘暴	Duststorm
// 29	强沙尘暴	Sandstorm
// 30	雾	Foggy
// 31	霾	Haze
// 32	风	Windy
// 33	大风	Blustery
// 34	飓风	Hurricane
// 35	热带风暴	Tropical Storm
// 36	龙卷风	Tornado
// 37	冷	Cold
// 38	热	Hot
// 99	未知	Unknown

// 存储时全部+1, 避免0值影响!!!

// 晴（国内城市白天晴）晴（国内城市夜晚晴）晴（国外城市白天晴）晴（国外城市夜晚晴）多云晴间多云晴间多云大部多云大部多云阴阵雨雷阵雨雷阵雨伴有冰雹小雨中雨大雨暴雨大暴雨特大暴雨冻雨雨夹雪阵雪小雪中雪大雪暴雪浮尘扬沙沙尘暴强沙尘暴雾霾风大风飓风热带风暴龙卷风冷热未知
weather_code_chinese_t w_chn_codes[] = {
    // {0, "晴（国内城市白天晴）"},
    // {1, "晴（国内城市夜晚晴）"},
    // {2, "晴（国外城市白天晴）"},
    // {3, "晴（国外城市夜晚晴）"},
    {0, "晴"},
    {1, "晴"},
    {2, "晴"},
    {3, "晴"},
    {4, "多云"},
    {5, "晴间多云"},
    {6, "晴间多云"},
    {7, "大部多云"},
    {8, "大部多云"},
    {9, "阴"},
    {10, "阵雨"},
    {11, "雷阵雨"},
    {12, "雷阵雨伴有冰雹"},
    {13, "小雨"},
    {14, "中雨"},
    {15, "大雨"},
    {16, "暴雨"},
    {17, "大暴雨"},
    {18, "特大暴雨"},
    {19, "冻雨"},
    {20, "雨夹雪"},
    {21, "阵雪"},
    {22, "小雪"},
    {23, "中雪"},
    {24, "大雪"},
    {25, "暴雪"},
    {26, "浮尘"},
    {27, "扬沙"},
    {28, "沙尘暴"},
    {29, "强沙尘暴"},
    {30, "雾"},
    {31, "霾"},
    {32, "风"},
    {33, "大风"},
    {34, "飓风"},
    {35, "热带风暴"},
    {36, "龙卷风"},
    {37, "冷"},
    {38, "热"},
    {99, "未知"},
};

int w_chn_codes_len = sizeof(w_chn_codes) / sizeof(weather_code_chinese_t);

weather_info_t get_forecast_weather(const char *city)
{
  String url_xinzhi = WEATHER_URL_FORECAST;
  url_xinzhi = url_xinzhi + "?key=" + WEATHER_URL_KEY;
  url_xinzhi = url_xinzhi + "&location=" + city;
  url_xinzhi = url_xinzhi + "&language=" + WEATHER_URL_LANGUAGE_EN;
  url_xinzhi = url_xinzhi + "&unit=c&start=0&days=3"; // 非vip最多三天

  JsonDocument doc;

  HTTPClient http;
  weather_info_t r;

  http.begin(url_xinzhi);

  int httpGet = http.GET();
  if (httpGet > 0)
  {
    if (httpGet == HTTP_CODE_OK)
    {
      String json = http.getString();
      // Serial.println(json); // 打印接受到的消息

      deserializeJson(doc, json);
      serializeJsonPretty(doc, Serial); // 这行也是打印接受到的消息

      JsonObject root = doc.as<JsonObject>();
      JsonArray results = root["results"];

      for (int i = 0; i < 3; ++i)
      {
        r.weathers[i].code = results[0]["daily"][i]["code_day"];
        r.weathers[i].precip = results[0]["daily"][i]["precip"].as<float>();
        r.weathers[i].wind_scale = results[0]["daily"][i]["wind_scale"].as<int>();

        strcpy(r.weathers[i].date, results[0]["daily"][i]["date"].as<const char *>());
        strcpy(r.weathers[i].text, results[0]["daily"][i]["text_day"].as<const char *>());
        strcpy(r.weathers[i].temp, results[0]["daily"][i]["high"].as<const char *>());
        strcpy(r.weathers[i].temp_low, results[0]["daily"][i]["low"].as<const char *>());
        strcpy(r.weathers[i].temp_high, results[0]["daily"][i]["high"].as<const char *>());
        strcpy(r.weathers[i].humidity, results[0]["daily"][i]["humidity"].as<const char *>());
        strcpy(r.weathers[i].city, results[0]["location"]["name"].as<const char *>());

        // printf("raw deserial data %s, %s %s -------\r\n", r.weathers[i].temp_high, r.weathers[i].temp_low, r.weathers[i].humidity);
        // r.weathers[i].temp = results[0]["daily"][i]["high"];
        // r.weathers[i].temp_low = results[0]["daily"][i]["low"];
        // r.weathers[i].temp_high = results[0]["daily"][i]["high"];
        // r.weathers[i].humidity = results[0]["daily"][i]["humidity"];
      }
    }
    else
    {
      String json = http.getString();
      printf("request fail: %d, %s\r\n", httpGet, json);
    }
  }
  else
  {
    Serial.printf("[HTTP] GET %s... failed, error: %s\n", url_xinzhi.c_str(), http.errorToString(httpGet).c_str());
  }
  http.end();

  return r;
}
const image_t *get_image_by_code(int code)
{
  switch (code)
  {
  case 0: case 1: case 2: case 3:
    return &img_sunny;
  case 4: case 5: case 6: case 7: case 8: case 9:
    return &img_cloudy;
  case 10: case 13:
    return &img_rainy_light;
  case 14:
    return &img_rainy_moderate;
  case 15:
    return &img_rainy_heavy;
  case 11: case 12: case 16: case 17: case 18:
    return &img_rainy_storm;
  case 19:case 20:case 21:case 22:
    return &img_snowy_light;
  case 23:
    return &img_snowy_moderate;
  case 24:
    return &img_snowy_heavy;
  case 25:
    return &img_snowy_storm;
  case 32:case 33:case 34: case 35:case 36:
    return &img_windy;
  default:
    return &img_cloudy;
  }
}